# coding=utf-8
from django.db.models.query_utils import Q
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from trp.models import *
from utils.JsonResponse import *
from django.shortcuts import render_to_response
import datetime
from django.core.exceptions import ObjectDoesNotExist
import json
from django.http.response import HttpResponse
from utils.decorators import *
import time
from django.db.models import Min,Max,Sum 
from website.settings import LOGGER as logger


#根据日期和媒体类型，获取商铺和运营商在某一天的预售收入
def get_cost_by_date(date,type):
    total_cost=0
    kwargs={}
    if type != None and type!="":
        kwargs["type"]=int(type)
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost

#组织某天的预售收入
def get_cost_by_org(date,org_id,type):
    total_cost=0
    kwargs={}
    if org_id:
        kwargs["organization_id"]=int(org_id)
    if type != None and type!="":
        kwargs["type"]=type
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost


def get_cost_by_strategy(date,sid,pid,type):
    total_cost=0    
    kwargs={}
    
    
    sid=int(sid)
    ids=ResourcePrice.objects.filter(strategy_id=sid,resource__deleted=False,resource__allow_shared=False).values_list("resource_id",flat=True)
    kwargs["resource_id__in"]=ids
    if pid:
        pid=int(pid)
        if pid>0:
            kwargs["resource__traffic__position_id"]=pid
    if type != None and type!="":
        type=int(type)
        kwargs["resource__traffic__position__media__type"]=type
        
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    strategy_resource_reports=DailyStrategyResourceReport.objects.filter(strategy_id=sid,resource__start__lte=date,resource__end__gte=date,**kwargs)
    resource_report=DailyResourceReport.objects.filter(resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:        
        resource=obj.resource
        rid=resource.id
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        reports=strategy_resource_reports.filter(resource_id=rid,date=date)
        strategy_resource_pv=0
        resource_pv=0
        if reports:
            strategy_resource_pv=reports[0].pv
        reports=resource_report.filter(resource_id=rid,date=date)
        if reports:
            resource_pv=reports[0].pv
        if resource_pv!=0:
            cost=(balance/float(days))*strategy_resource_pv/float(resource_pv)
            total_cost+=cost
    return total_cost


#根据日期，获取指定媒体在某天的预售收入
def get_cost_by_media(date,media_id):
    total_cost=0   
    kwargs={}    
    if media_id:
        media_id=int(media_id)
        kwargs["resource__traffic__position__media__id"]=media_id
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost

def get_cost_by_position(date,pid):
    total_cost=0   
    kwargs={}    
    if pid:
        pid=int(pid)
        kwargs["resource__traffic__position__id"]=pid
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost
    

def get_cost_by_traffic(date,tid):
    total_cost=0    
    kwargs={}
    
    if tid:
        tid=int(tid)
        kwargs["resource__traffic__id"]=tid
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost
#分片的预售收入
def get_cost_by_resource(date,rid):
    total_cost=0
    start=datetime.datetime(date.year,date.month,date.day)
    end=datetime.datetime(date.year,date.month,date.day,23,59,59)
    kwargs={}
    
    if rid:
        rid=int(rid)
        kwargs["resource__id"]=rid   
    objs=CostLog.objects.filter(deleted=False,resource__start__lte=date,resource__end__gte=date,**kwargs)
    for obj in objs:
        resource=obj.resource
        start=resource.start
        end=resource.end
        start = datetime.datetime(start.year, start.month, start.day)
        end = datetime.datetime(end.year, end.month, end.day)
        balance=obj.balance       
        days=(end-start).days+1
        cost=balance/float(days)
        total_cost+=cost
    return total_cost
 #分片的媒体花费       
def get_media_cost_by_resource(date,rid,pv,position_pv):
    cpd=0
    cpm=0 
    rid=int(rid)
    resource=Resource.objects.get(pk=rid)
    traffic=resource.traffic
    bid_type=traffic.bid_type
    bid_price=traffic.bid_price
    if bid_type==0 and position_pv!=0:
        cpd+=(pv*bid_price)/float(position_pv)
    else:         
        cpm+=(pv*bid_price)/float(1000)
    sum=cpd+cpm
    return sum
     
     
     
     
     
     
     
     
     
     
     
     
                